
<!DOCTYPE html>
<html lang="">


<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no">
  <meta name="theme-color" content="#202020"/>
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  <script src="/js/busuanzi.pure.mini.js" async></script>
  
  
    <meta name="keywords" content="Emberjs,Ember-Demo," />
  

  
    <meta name="description" content="一个专注一coding的网站。提供丰富编程知识，包括Spring、Java、EmberJS、SpringBoot等等技术。" />
  
  
  <link rel="icon" type="image/x-icon" href="/image/favicon.ico">
  <title>环境搭建以及使用Ember.js创建第一个静态页面 [ Keep Coding ]</title>
  
    <!-- stylesheets list from config.yml -->
    
      <link rel="stylesheet" href="/css/pure-min.css">
    
      <link rel="stylesheet" href="/css/xoxo.css">
    
  
<meta name="generator" content="Hexo 5.0.2"><link rel="alternate" href="/atom.xml" title="Keep Coding" type="application/atom+xml">
</head>

<body>
  <div class="nav-container">
    <nav class="home-menu pure-menu pure-menu-horizontal">
  <a class="pure-menu-heading" href="/">
    <img class="avatar" src="/image/favicon.ico">
    <span class="title">Keep Coding</span>
  </a>

  <ul class="pure-menu-list clearfix">
      
          
            <li class="pure-menu-item"><a href="/" class="pure-menu-link">首页</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/archives" class="pure-menu-link">归档</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/tags" class="pure-menu-link">标签</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/search" class="pure-menu-link">搜索</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/about" class="pure-menu-link">关于</a></li>
          
      
          
            <li class="pure-menu-item"><a href="/atom.xml" class="pure-menu-link">订阅</a></li>
          
      
  </ul>
   
</nav>
  </div>

  <div class="container" id="content-outer">
    <div class="inner" id="content-inner">
      <div class="post-container">
  <article class="post" id="post">
    <header class="post-header text-center">
      <h1 class="title">
        环境搭建以及使用Ember.js创建第一个静态页面
      </h1>
      <span>
        
        <time class="time" datetime="2020-01-18T14:50:36.000Z">
        2020-01-18
      </time>
        
      </span>
      <span class="slash">/</span>
      <span class="post-meta">
      <span class="post-tags">
        <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Ember-Demo/" rel="tag">Ember-Demo</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Emberjs/" rel="tag">Emberjs</a></li></ul>
      </span>
    </span>
      <span class="slash">/</span>
      <span class="read">
      <span id="busuanzi_value_page_pv"></span> 点击
    </span>
      <span class="slash">/</span>
      <span class="read">阅读耗时 4 分钟</span>
    </header>

    <div class="post-content">
      <p>来源：<a target="_blank" rel="noopener" href="http://yoember.com/">yoember.com</a><br>作者：<a target="_blank" rel="noopener" href="http://zoltan.nz/">Zoltan</a></p>
<p><strong>声明</strong>：<em>本文的转载与翻译是经过作者认可的，再次感谢原作，如有侵权请给我留言，我会删除博文！！</em> 希望本系列教程能帮助更多学习Ember.js的初学者。</p>
<p>本篇将为读者介绍Ember项目开发环境的搭建，并创建一个静态页面。</p>
<h2 id="安装Ember-CLI"><a href="#安装Ember-CLI" class="headerlink" title="安装Ember CLI"></a>安装Ember CLI</h2><p>本教程使用的是<code>2.4.3</code>版本的Ember CLI工具集，如果你的是<code>1.13.8</code>版本在启动项目时会提示如下错误：<br><code>Future versions of Ember CLI will not support v5.9.1. Please update to Node 0.12 or io.js.</code><br>但是项目仍然可以正常访问，不过建议还是升级到<code>2.4.3</code>版本，省得出现未知错误不好解决。<br>升级命令：<code>npm install -g ember-cli@2.4.3</code></p>
<p>查看<code>ember</code>命令是否安装成功，在终端或者控制台下输入下面的命令</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ember -v</span><br></pre></td></tr></table></figure>
<p>如果出现如下信息说明环境搭建成功。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">version: 2.4.3</span><br><span class="line">node: 5.9.1</span><br><span class="line">npm: 2.13.4</span><br><span class="line">os: darwin x64</span><br></pre></td></tr></table></figure>
<p>如果你用的电脑不是Mac最后一行os有所不同，这个不要紧。第一行是<code>Ember CLI</code>的版本号，第二行是<code>node</code>的版本号，第三行是<code>npm</code>的版本号，最后一个是系统版本。</p>
<p><strong>注意</strong></p>
<p>如果你的执行<code>ember -v</code>得不到上述的版本信息也不要紧，仍然按照下面的教程新建项目新建完成项目之后再更行Ember CLI的版本，更新教程请参考<a target="_blank" rel="noopener" href="https://github.com/ember-cli/ember-cli/releases">Could this be a shame in the making?</a>，只需要根据<strong>Project Update</strong>部分更新项目即可，更新到最后一步<code>ember init</code>时候会如下确认信息，全部<code>y</code>即可。</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">? Overwrite .travis.yml? Yes, overwrite</span><br><span class="line">? Overwrite .watchmanconfig? Yes, overwrite</span><br><span class="line">? Overwrite README.md? Yes, overwrite</span><br><span class="line">? Overwrite app/app.js? Yes, overwrite</span><br><span class="line">? Overwrite app/index.html? Yes, overwrite</span><br><span class="line">? Overwrite app/router.js? Yes, overwrite</span><br><span class="line">? Overwrite bower.json? Yes, overwrite</span><br><span class="line">? Overwrite ember-cli-build.js? Yes, overwrite</span><br><span class="line">? Overwrite package.json? Yes, overwrite</span><br><span class="line">? Overwrite tests/helpers/resolver.js? Yes, overwrite</span><br><span class="line">? Overwrite tests/helpers/start-app.js? Yes, overwrite</span><br><span class="line">? Overwrite tests/index.html? Yes, overwrite</span><br></pre></td></tr></table></figure>

<p>更新过程可能还会出现如下选择版本的问题，请根据下面例子选择：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">Installed packages for tooling via npm.</span><br><span class="line">  conflict Unable to find suitable version for qunit-notifications</span><br><span class="line">    1) qunit-notifications ~0.0.6</span><br><span class="line">    2) qunit-notifications ~0.1.0</span><br><span class="line">? Answer 2</span><br><span class="line">  conflict Unable to find suitable version for ember</span><br><span class="line">    1) ember &gt;= 1.8.1 &lt; 2.0.0</span><br><span class="line">    2) ember &gt;=1.4 &lt;2</span><br><span class="line">    3) ember &gt; 1.5.0-beta.3</span><br><span class="line">    4) ember ~2.4.3</span><br><span class="line">    5) ember &gt;=1.4</span><br><span class="line">? Answer 4</span><br><span class="line">Installed browser packages via Bower.</span><br></pre></td></tr></table></figure>
<p>最后验证是否更新成功，执行<code>ember -v</code>会得到如下版本信息：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">ubuntuvimdeMacBook-Pro:library-app ubuntuvim$ ember -v</span><br><span class="line">ember-cli: 2.4.3</span><br><span class="line">node: 5.9.1</span><br><span class="line">os: darwin x64</span><br></pre></td></tr></table></figure>

<p>更多有关开发环境的详细介绍请看<a target="_blank" rel="noopener" href="http://ember-cli.com/user-guide">www.ember-cli.com</a>。</p>
<h2 id="创建一个新项目"><a href="#创建一个新项目" class="headerlink" title="创建一个新项目"></a>创建一个新项目</h2><p>安装好开发环境之后，直接使用<a target="_blank" rel="noopener" href="http://ember-cli.com/user-guide">Ember CLI</a>命令创建新项目。下面是创建命令：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ember new library-app</span><br></pre></td></tr></table></figure>
<p>等待命令执行完成，安装过程需要下载所必须的npm插件，跟网络有关系，请耐心等待。</p>
<h2 id="运行项目"><a href="#运行项目" class="headerlink" title="运行项目"></a>运行项目</h2><p>等待项目创建完成之后就可以直接使用命令运行项目了，首先进入项目目录下，然后执行ember cli命令运行项目。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F;  进入项目目录下</span><br><span class="line">cd library-app</span><br><span class="line">&#x2F;&#x2F;  执行启动命令</span><br><span class="line">ember server</span><br></pre></td></tr></table></figure>
<p><em><code>//</code>的内容为注释，请直接忽略。</em></p>
<p>等待启动完毕后，打开浏览器执行<a target="_blank" rel="noopener" href="http://localhost:4200/">http://localhost:4200</a>，如果能在页面上看到<strong>Welcome to Ember</strong>说明项目创建成功。并且可以在浏览器控制台上看到如下图的日志信息：</p>
<p><img src="/image/blog-image/55.png" alt="日志信息"></p>
<h2 id="开启调试模式"><a href="#开启调试模式" class="headerlink" title="开启调试模式"></a>开启调试模式</h2><p>在开发阶段最好是把打开调试模式，开启之后可以在浏览器的控制台下看到ember项目执行过程的相关信息，有助于发现问题。<br>修改<code>library-app/config/environment.js</code>文件的内容，在下面代码段中增加配置：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F; ……</span><br><span class="line">if (environment &#x3D;&#x3D;&#x3D; &#39;development&#39;) &#123;</span><br><span class="line">  &#x2F;&#x2F; ENV.APP.LOG_RESOLVER &#x3D; true;</span><br><span class="line">  ENV.APP.LOG_ACTIVE_GENERATION &#x3D; true;</span><br><span class="line">  ENV.APP.LOG_TRANSITIONS &#x3D; true;</span><br><span class="line">  ENV.APP.LOG_TRANSITIONS_INTERNAL &#x3D; true;</span><br><span class="line">  ENV.APP.LOG_VIEW_LOOKUPS &#x3D; true;</span><br><span class="line">&#125;</span><br><span class="line">&#x2F;&#x2F;……</span><br></pre></td></tr></table></figure>
<p>重启项目（按<code>Ctrl+C</code>终止在执行<code>ember servere</code>），必须重启才能其效果，可以在浏览器控制台看到了很多的日志信息。比如下图</p>
<p><img src="/image/blog-image/56.png" alt="开启日志模式截图"></p>
<h2 id="添加Bootstrap和Sass到项目中"><a href="#添加Bootstrap和Sass到项目中" class="headerlink" title="添加Bootstrap和Sass到项目中"></a>添加Bootstrap和Sass到项目中</h2><p>为了美化项目界面引入<a target="_blank" rel="noopener" href="http://www.bootcss.com/">Bootstrap</a>，这两个插件的安装也是直接使用Ember CLI命令安装，命令如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">ember install ember-cli-sass</span><br><span class="line">ember install ember-cli-bootstrap-sassy</span><br></pre></td></tr></table></figure>
<p>等待安装完成之后可以在项目目录下的<code>pachage.json</code>和<code>bower.json</code>看到这两个插件的配置信息。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&#x2F;&#x2F;  bower.json</span><br><span class="line">&quot;bootstrap-sass&quot;: &quot;^3.3.6&quot;</span><br><span class="line">&#x2F;&#x2F; package.json</span><br><span class="line">&quot;ember-cli-sass&quot;: &quot;5.3.1&quot;</span><br></pre></td></tr></table></figure>

<p><strong>在项目下增加样式文件</strong></p>
<p>创建文件<code>library-app/app/styles/app.scss</code>，如果项目已经存在文件<code>library-app/app/styles/app.css</code>则重命名为<code>app.scss</code>，样式会被Ember CLI引入到项目中。<br>在文件中增加如下内容：</p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">@import</span> <span class="string">&quot;bootstrap&quot;</span>;</span><br></pre></td></tr></table></figure>
<p>使用快捷键<code>Ctrl+C</code>关闭在用命令<code>ember server</code>启动项目。如果终端没出现错误说明配置是正确的。那么请继续往下看！！</p>
<h2 id="创建项目导航条"><a href="#创建项目导航条" class="headerlink" title="创建项目导航条"></a>创建项目导航条</h2><p>在前面引入的了<a target="_blank" rel="noopener" href="http://www.bootcss.com/">Bootstrap</a>之后我们就可以在页面中直接使用了，并且不需要再在页面上引入相关的<code>css</code>和<code>js</code>文件。<br>打开文件<code>library-app/app/templates/application.hbs</code>，清空原有代码再添加如下代码：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;container&quot;</span>&gt;</span></span><br><span class="line">    &#123;&#123;partial &#x27;navbar&#x27;&#125;&#125;</span><br><span class="line">    &#123;&#123;outlet&#125;&#125;</span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>Ember.js项目的页面使用的是<a target="_blank" rel="noopener" href="http://handlebarsjs.com/">Handlebarsjs</a>模板，<code>{{}}`是模板的语法。在Ember.js的官方参考教程中有一章是专门介绍如何使用Handlebarsjs模板的，或者根据[Ember.js 入门指南之八handlebars基础](http://blog.ddlisting.com/2016/03/18/ember-js-ru-men-zhi-nan-zhi-ba-handlebarsji-chu/)学习。
在上述代码中`{{partial}}</code>是一个<a target="_blank" rel="noopener" href="https://guides.emberjs.com/v1.13.0/templates/rendering-with-helpers/">ember helper</a>可以用于调用模板，这里就是调用了模板<code>navbar</code>，不过这个功能在<code>2.4</code>的参考文档中移除了可以在<code>1.13.0</code>的文档中看到，更多有关信息请看参考网址。<br>代码中<code>{{outlet}}</code>也是一个helper，但是这个是一个特殊的helper，你可以把这个helper理解为一个占位符。所有子模板都会渲染到<code>{{outlet}}</code>所在的位置。更多信息请看<a target="_blank" rel="noopener" href="http://blog.ddlisting.com/2016/03/22/ember-js-ru-men-zhi-nan-zhi-shi-si-fan-wai-pian-lu-you-mo-ban-de-zhi-xing-xuan-ran-shun-xu/">Ember.js 入门指南之十四番外篇，路由、模板的执行、渲染顺序</a>。</p>
<p><strong>创建一个模板</strong></p>
<p>仍然是使用命令创建模板。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ember g template navbar</span><br></pre></td></tr></table></figure>
<p>等待命令执行完毕之后可以看到<code>library-app/app/templates/navbar.hbs</code>这个文件。下面在文件中增加一个导航条。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">nav</span> <span class="attr">class</span>=<span class="string">&quot;navbar navbar-inverse&quot;</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;container-fluid&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;navbar-header&quot;</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">button</span> <span class="attr">type</span>=<span class="string">&quot;button&quot;</span> <span class="attr">class</span>=<span class="string">&quot;navbar-toggle collapsed&quot;</span> <span class="attr">data-toggle</span>=<span class="string">&quot;collapse&quot;</span> <span class="attr">data-target</span>=<span class="string">&quot;#main-navbar&quot;</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">span</span> <span class="attr">class</span>=<span class="string">&quot;sr-only&quot;</span>&gt;</span>Toggle navigation<span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">span</span> <span class="attr">class</span>=<span class="string">&quot;icon-bar&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">span</span> <span class="attr">class</span>=<span class="string">&quot;icon-bar&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">span</span> <span class="attr">class</span>=<span class="string">&quot;icon-bar&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">span</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;/<span class="name">button</span>&gt;</span></span><br><span class="line">      &#123;&#123;#link-to &#x27;index&#x27; class=&quot;navbar-brand&quot;&#125;&#125;Library App&#123;&#123;/link-to&#125;&#125;</span><br><span class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;collapse navbar-collapse&quot;</span> <span class="attr">id</span>=<span class="string">&quot;main-navbar&quot;</span>&gt;</span></span><br><span class="line">      <span class="tag">&lt;<span class="name">ul</span> <span class="attr">class</span>=<span class="string">&quot;nav navbar-nav&quot;</span>&gt;</span></span><br><span class="line">            &#123;&#123;#link-to &#x27;index&#x27; tagName=&quot;li&quot;&#125;&#125;<span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">&quot;&quot;</span>&gt;</span>Home<span class="tag">&lt;/<span class="name">a</span>&gt;</span>&#123;&#123;/link-to&#125;&#125;</span><br><span class="line">      <span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">div</span>&gt;</span><span class="comment">&lt;!-- /.navbar-collapse --&gt;</span></span><br><span class="line">  <span class="tag">&lt;/<span class="name">div</span>&gt;</span><span class="comment">&lt;!-- /.container-fluid --&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">nav</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>代码中<code>{{link-to}}</code>是Handlebars模板的标签，在第一个<code>{{link-to}}</code>标签中<code>index</code>是一个路由的名字，模板被编译之后这个标签就转成一个普遍HTML标签的<code>&lt;a&gt;</code>，如果你想指定编译之后的标签名请使用属性<code>tagName</code>指定，比如上述代码的第二个<code>link-to</code>标签，在后面的文章中会使用组件（<code>component</code>）重构这个标签。<br><em>为了美化界面在页面的顶部加了css的填充，修改样式文件<code>app.scss</code>。</em></p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">@import</span> <span class="string">&quot;bootstrap&quot;</span>;</span><br><span class="line"></span><br><span class="line"><span class="selector-tag">body</span> &#123;</span><br><span class="line">    <span class="attribute">padding-top</span>: <span class="number">20px</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>等待项目重启完成，可以在页面上看到黑色的导航条，好像我们并且没有在任何地方使用这个模板<code>navbar</code>，为何能在首页上显示呢？？其实我们已经在<code>application.hbs</code>中调用了！在这个模板中有这样一句代码<code>{{partial 'navbar'}}</code>，在此根据模板名调用了模板<code>navbar</code>。如果删除了<code>application.hbs</code>中的<code>{{partial}}</code>界面上就什么都不显示了！请读者自行实验。</p>
<h2 id="创建关于界面并在导航菜单上增加一个菜单项"><a href="#创建关于界面并在导航菜单上增加一个菜单项" class="headerlink" title="创建关于界面并在导航菜单上增加一个菜单项"></a>创建关于界面并在导航菜单上增加一个菜单项</h2><p>同样的，使用Ember CLI命令创建一个路由（route），有关路由的信息可以查看官方参考文档或者直接看教程<a target="_blank" rel="noopener" href="http://blog.ddlisting.com/2016/03/25/ember-js-ru-men-zhi-nan-zhi-er-shi-lu-you-ding-yi/">Ember.js 入门指南之二十路由定义</a>，文章上有详细的介绍，欢迎阅读！执行下面的命令创建路由，创建路由的过程中会同时创建路由对应的模板，所以执行一个命令会得到2个文件：<code>app/templates/about.hbs</code>、<code>app/routes/about.js</code>，同时会在<code>app/router.js</code>中app/自动增加一条路由配置语句<code>this.route(&#39;about&#39;);</code>。然后在模板<code>about.hbs</code>中增加一些信息：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">&#123;&#123;! app/templates/about.hbs &#125;&#125;</span><br><span class="line"><span class="tag">&lt;<span class="name">h1</span>&gt;</span>About Page<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>等待项目重启完成，执行<a target="_blank" rel="noopener" href="http://localhost:4200/about">http://localhost:4200/about</a>可以看到刚刚在模板<code>about.hbs</code>中增加的信息，如下截图。</p>
<p><img src="/image/blog-image/81.png" alt="结果截图"></p>
<p>但是”About Page”怎么会显示在导航条下方呢？好像并没有指定啊，也没有想前面那样使用表达式<code>{{partial}}</code>调用模板，有关这个内容的介绍请看<a target="_blank" rel="noopener" href="http://blog.ddlisting.com/2016/03/22/ember-js-ru-men-zhi-nan-zhi-shi-si-fan-wai-pian-lu-you-mo-ban-de-zhi-xing-xuan-ran-shun-xu/">Ember.js 入门指南之十四番外篇，路由、模板的执行、渲染顺序</a>，还记得在主模板<code>application.hbs</code>中的<code>{{outlet}}</code>吗？除了<code>application.hbs</code>之外的所有模板都是子模板，子模板会自动渲染到父模板的<code>{{outlet}}</code>上。但是是如何触发显示的呢？很简单，因为我们访问了<code>about</code>这个路由，路由会自动根据名字查找到同名的模板并显示（Ember默认规则）。</p>
<p>再创建一个模板<code>index</code>，仍然是使用Ember CLI命令创建，执行命令：<code>ember g template index</code>，得到模板后再模板内添加一些内容：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">&#123;&#123;! app/templates/index.hbs&#125;&#125;</span><br><span class="line"><span class="tag">&lt;<span class="name">h1</span>&gt;</span>Home Page<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>然后执行<a target="_blank" rel="noopener" href="http://localhost:4200/">http://localhost:4200/</a>，神奇的事情发生了，可以直接看到模板<code>index</code>的内容，并且并没有访问<a target="_blank" rel="noopener" href="http://localhost:4200/index">http://localhost:4200/index</a>。这又是为什么呢？请看<a target="_blank" rel="noopener" href="http://blog.ddlisting.com/2016/03/25/ember-js-ru-men-zhi-nan-zhi-er-shi-lu-you-ding-yi/">Ember.js 入门指南之二十路由定义</a>中关于<code>index</code>路由的解释。简单讲，<code>index</code>路由就是每个路由默认首页路由，不需要手动创建，这个路由对应的URL是<code>/</code>，当你执行<a target="_blank" rel="noopener" href="http://localhost:4200/">http://localhost:4200/</a>时候实际就是执行<a target="_blank" rel="noopener" href="http://localhost:4200/index">http://localhost:4200/index</a>然后渲染的模板就是<code>index.hbs</code>，所以就得到界面显示的效果。<br>然后在导航栏上在添加一个链接，最后得打如下代码（前后部分代码省略）：</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">ul</span> <span class="attr">class</span>=<span class="string">&quot;nav navbar-nav&quot;</span>&gt;</span></span><br><span class="line">            &#123;&#123;#link-to &#x27;index&#x27; tagName=&quot;li&quot;&#125;&#125;<span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">&quot;&quot;</span>&gt;</span>Home<span class="tag">&lt;/<span class="name">a</span>&gt;</span>&#123;&#123;/link-to&#125;&#125;</span><br><span class="line">            &#123;&#123;#link-to &#x27;about&#x27; tagName=&quot;li&quot;&#125;&#125;<span class="tag">&lt;<span class="name">a</span> <span class="attr">href</span>=<span class="string">&quot;&quot;</span>&gt;</span>About<span class="tag">&lt;/<span class="name">a</span>&gt;</span>&#123;&#123;/link-to&#125;&#125;</span><br><span class="line">      <span class="tag">&lt;/<span class="name">ul</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>等待项目重启完成，可以看到导航栏上多了一项，并且点击“Home”和“About”看到显示不同的内容。效果如下图：</p>
<p><img src="/image/blog-image/81.png" alt="效果图"></p>
<p>到此教程第一篇介绍完毕，如果你看过官方参考文件或者是看过<a target="_blank" rel="noopener" href="http://blog.ddlisting.com/">ember teach</a>上的教程理解起来应该是没难度的！多一份耐心就多一份收获。</p>
<h2 id="家庭作业"><a href="#家庭作业" class="headerlink" title="家庭作业"></a>家庭作业</h2><p>最后给你留了一份作业，想学好就必须要动手实践才行啊！！！</p>
<p>作业内容：</p>
<ol>
<li>创建一个名为<code>contact</code>的路由和模板</li>
<li>在导航菜单上增加一个菜单项”Contact”，并且点击这个菜单项看到的是模板<code>contact.hbs</code>的内容。</li>
</ol>
<br>
为了照顾懒人我把完整的代码放在[GitHub](https://github.com/ubuntuvim/library-app)上，如有需要请参考参考。博文经过多次修改，博文上的代码与github代码可能有出入，不过影响不大！如果你觉得博文对你有点用，请在github项目上给我点个`star`吧。您的肯定对我来说是最大的动力！！

    </div>

      
    <div class="post-nav">
      <div class="post-nav-item post-nav-next">
        
          <span>〈 </span>
          <a href="/2020/01/18/Ember.js/Ember.js%E6%8F%92%E4%BB%B6/" rel="next" title="Ember.js插件">
          Ember.js插件
          </a>
        
      </div>
  
      <div class="post-nav-item post-nav-prev">
          
          <a href="/2020/01/18/Ember.js/Ember.js%E4%BB%A3%E7%A0%81%E5%A4%87%E5%BF%98/" rel="prev" title="Ember.js代码备忘">
            Ember.js代码备忘
          </a>
          <span>〉</span>
        
      </div>
    </div>
  
  </article>
  <div class="toc-container">
    
  <div id="toc" class="toc-article">
    <strong class="toc-title">目录</strong>
    <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AE%89%E8%A3%85Ember-CLI"><span class="toc-text">安装Ember CLI</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%88%9B%E5%BB%BA%E4%B8%80%E4%B8%AA%E6%96%B0%E9%A1%B9%E7%9B%AE"><span class="toc-text">创建一个新项目</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%BF%90%E8%A1%8C%E9%A1%B9%E7%9B%AE"><span class="toc-text">运行项目</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%BC%80%E5%90%AF%E8%B0%83%E8%AF%95%E6%A8%A1%E5%BC%8F"><span class="toc-text">开启调试模式</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%B7%BB%E5%8A%A0Bootstrap%E5%92%8CSass%E5%88%B0%E9%A1%B9%E7%9B%AE%E4%B8%AD"><span class="toc-text">添加Bootstrap和Sass到项目中</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%88%9B%E5%BB%BA%E9%A1%B9%E7%9B%AE%E5%AF%BC%E8%88%AA%E6%9D%A1"><span class="toc-text">创建项目导航条</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%88%9B%E5%BB%BA%E5%85%B3%E4%BA%8E%E7%95%8C%E9%9D%A2%E5%B9%B6%E5%9C%A8%E5%AF%BC%E8%88%AA%E8%8F%9C%E5%8D%95%E4%B8%8A%E5%A2%9E%E5%8A%A0%E4%B8%80%E4%B8%AA%E8%8F%9C%E5%8D%95%E9%A1%B9"><span class="toc-text">创建关于界面并在导航菜单上增加一个菜单项</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AE%B6%E5%BA%AD%E4%BD%9C%E4%B8%9A"><span class="toc-text">家庭作业</span></a></li></ol>
  </div>


  </div>
</div>


<div class="copyright">
    <span>本作品采用</span>
    <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by/4.0/">知识共享署名 4.0 国际许可协议</a>
    <span>进行许可。 转载时请注明原文链接。</span>
</div>
<div class="share">

</div>
<div class="post-container">
    <article class="post">
      <div id="container"></div>
    </article>
</div>

<link rel="stylesheet" href="https://imsun.github.io/gitment/style/default.css">
<script src="https://imsun.github.io/gitment/dist/gitment.browser.js"></script>
<script>
var gitment = new Gitment({
  id: '环境搭建以及使用Ember.js创建第一个静态页面', // 可选。默认为 location.href
  owner: 'ubuntuvim',
  repo: 'ubuntuvim.github.io',
  oauth: {
    client_id: '48b3df0bf4ba1743e0a3',
    client_secret: 'c6c2cec09acb5eecc9d325d56cef187264aa1e16',
  },
})
gitment.render('container')
</script>



    </div>

    

  </div>
  <footer class="footer text-center">
    <div id="bottom-inner">
        <a class="bottom-item" href="http://xcoding.tech/">首页</a> |
        <a class="bottom-item" href="http://xcoding.tech/" target="_blank">主站</a> |
        <a class="bottom-item" href="https://github.com/ubuntuvim" target="_blank">GitHub</a> |
        <a class="bottom-item" href="https://hexo.io" target="_blank">Powered by hexo</a> |
        <a class="bottom-item" href="https://github.com/KevinOfNeu/hexo-theme-xoxo" target="_blank">Theme xoxo</a>
    </div>
</footer>


  <script src='https://unpkg.com/mermaid@7.1.2/dist/mermaid.min.js'></script>
  <script>
    if (window.mermaid) {
      mermaid.initialize({theme: 'forest'});
    }
  </script>


  
  <!-- scripts list from theme config.yml -->
  
    <script src="/js/zepto.min.js"></script>
  


<script>
  (function(window, document, undefined) {

    var timer = null;

    function returnTop() {
      cancelAnimationFrame(timer);
      timer = requestAnimationFrame(function fn() {
        var oTop = document.body.scrollTop || document.documentElement.scrollTop;
        if (oTop > 0) {
          document.body.scrollTop = document.documentElement.scrollTop = oTop - 50;
          timer = requestAnimationFrame(fn);
        } else {
          cancelAnimationFrame(timer);
        }
      });
    }

    var hearts = [];
    window.requestAnimationFrame = (function() {
      return window.requestAnimationFrame ||
        window.webkitRequestAnimationFrame ||
        window.mozRequestAnimationFrame ||
        window.oRequestAnimationFrame ||
        window.msRequestAnimationFrame ||
        function(callback) {
          setTimeout(callback, 1000 / 60);
        }
    })();
    init();

    function init() {
      css(".heart{z-index:9999;width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: absolute;}.heart:after{top: -5px;}.heart:before{left: -5px;}");
      attachEvent();
      gameloop();
      addMenuEvent();
    }

    function gameloop() {
      for (var i = 0; i < hearts.length; i++) {
        if (hearts[i].alpha <= 0) {
          document.body.removeChild(hearts[i].el);
          hearts.splice(i, 1);
          continue;
        }
        hearts[i].y--;
        hearts[i].scale += 0.004;
        hearts[i].alpha -= 0.013;
        hearts[i].el.style.cssText = "left:" + hearts[i].x + "px;top:" + hearts[i].y + "px;opacity:" + hearts[i].alpha + ";transform:scale(" + hearts[i].scale + "," + hearts[i].scale + ") rotate(45deg);background:" + hearts[i].color;
      }
      requestAnimationFrame(gameloop);
    }

    /**
     * 给logo设置点击事件
     * 
     * - 回到顶部
     * - 出现爱心
     */
    function attachEvent() {
      var old = typeof window.onclick === "function" && window.onclick;
      var logo = document.getElementById("logo");
      if (logo) {
        logo.onclick = function(event) {
          returnTop();
          old && old();
          createHeart(event);
        }
      }
      
    }

    function createHeart(event) {
      var d = document.createElement("div");
      d.className = "heart";
      hearts.push({
        el: d,
        x: event.clientX - 5,
        y: event.clientY - 5,
        scale: 1,
        alpha: 1,
        color: randomColor()
      });
      document.body.appendChild(d);
    }

    function css(css) {
      var style = document.createElement("style");
      style.type = "text/css";
      try {
        style.appendChild(document.createTextNode(css));
      } catch (ex) {
        style.styleSheet.cssText = css;
      }
      document.getElementsByTagName('head')[0].appendChild(style);
    }

    function randomColor() {
      // return "rgb(" + (~~(Math.random() * 255)) + "," + (~~(Math.random() * 255)) + "," + (~~(Math.random() * 255)) + ")";
      return "#F44336";
    }

    function addMenuEvent() {
      var menu = document.getElementById('menu-main-post');
      if (menu) {
        var toc = document.getElementById('toc');
        if (toc) {
          menu.onclick = function() {
            if (toc) {
              if (toc.style.display == 'block') {
                toc.style.display = 'none';
              } else {
                toc.style.display = 'block';
              }
            }
          };
        } else {
          menu.style.display = 'none';
        }
      }
    }

  })(window, document);
</script>

  



</body>
</html>
